/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package demochess;

import Helper.ChessMessageHandler;
import Helper.MessageHandlerInterface;
import Helper.SocketReceiver;
import Helper.SocketSender;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Nhattruong
 */
public class Main {

    public static final int _port = 31312;
    private static DataOutputStream os;
    private static SocketReceiver _sr;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        System.out.println("[W]ail for player!");
        System.out.println("[S]earch player!");
        System.out.print("Enter your choise: ");
        String op = sc.nextLine();
        switch (op.toLowerCase().charAt(0)) {
            case 'w':
                WaitForUser();
                break;
            case 's':
                SearchUser();
                break;
        }
    }
    static ServerSocket _srvSocket;
    static boolean _allowServerWait = true;
    static SocketReceiver sr;
    static SocketSender ss;

    public static void WaitForUser() {
        try {
            _srvSocket = new ServerSocket(_port);
            while (_allowServerWait) {
                try {
                    Socket aStaffSocket = _srvSocket.accept();
                    if (aStaffSocket != null) {
                        aStaffSocket = null;
                        _allowServerWait = false;
                    }
                } catch (Exception e) {
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private static void SearchUser() {
        try {
            Socket search = new Socket("127.0.0.1", _port);
            if (search != null) {
                os = new DataOutputStream(search.getOutputStream());
                os.writeBytes("Staff:");
                os.write(13);
                os.write(10);
                os.flush();
                Scanner sc = new Scanner(System.in);
                String tmp = "";
                do {
                    System.out.print("Your message: ");
                    tmp = sc.nextLine();
                    os = new DataOutputStream(search.getOutputStream());
                    os.writeBytes(tmp);
                    os.write(13);
                    os.write(10);
                    os.flush();
                } while (!tmp.equals("stop"));
            }

        } catch (UnknownHostException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
